package com.huangkailong.gitimgbed.core;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.URLDecoder;
import java.util.Objects;
import java.util.Properties;

/**
 * @author huangkl
 * @since 1.0.0
 */
@Slf4j
public class ImgBedConfiguration {
    private final String path;
    private volatile Properties properties;

    public ImgBedConfiguration(String path) {
        this.path = path;
    }

    /**
     * git 仓库 url
     */
    public String getGitRepositoryUrl() {
        return doGetProperty(Constants.KEY_GIT_REPOSITORY_URL);
    }

    /**
     * git 仓库分支
     */
    public String getGitRepositoryBranch() {
        return doGetProperty(Constants.KEY_GIT_REPOSITORY_BRANCH);
    }

    /**
     * git 仓库本地保存地址
     */
    public String getGitRepositoryLocalPath() {
        return doGetProperty(Constants.KEY_GIT_REPOSITORY_LOCAL_PATH);
    }

    /**
     * git 登录用户名
     */
    public String getGitUsername() {
        return doGetProperty(Constants.KEY_GIT_USERNAME);
    }

    /**
     * git 登录密码
     */
    public String getGitPasswd() {
        return doGetProperty(Constants.KEY_GIT_PASSWD);
    }

    /**
     * 获取图片 url 前缀
     */
    public String getImgUrlPrefix(){
        return doGetProperty(Constants.KEY_IMG_URL_PREFIX);
    }


    public void setGitRepositoryUrl(String gitRepositoryUrl) {
        doSetProperty(Constants.KEY_GIT_REPOSITORY_URL, gitRepositoryUrl);
    }


    public void setGitRepositoryBranch(String gitRepositoryBranch) {
        doSetProperty(Constants.KEY_GIT_REPOSITORY_BRANCH, gitRepositoryBranch);
    }


    public void setGitRepositoryLocalPath(String gitRepositoryLocalPath) {
        doSetProperty(Constants.KEY_GIT_REPOSITORY_LOCAL_PATH, gitRepositoryLocalPath);
    }


    public void setGitUsername(String gitUsername) {
        doSetProperty(Constants.KEY_GIT_USERNAME, gitUsername);
    }


    public void setGitPasswd(String gitPasswd) {
        doSetProperty(Constants.KEY_GIT_PASSWD, gitPasswd);
    }


    public void setImgUrlPrefix(String imgUrlPrefix){
        doSetProperty(Constants.KEY_IMG_URL_PREFIX, imgUrlPrefix);
    }

    private void doSetProperty(String key, String value){
        final Properties properties = getProperties();
        Objects.requireNonNull(properties);
        properties.setProperty(key, value);
        rewrite();
    }

    private String doGetProperty(String key){
        final Properties properties = getProperties();
        Objects.requireNonNull(properties);
        return properties.getProperty(key);
    }

    public void rewrite(){
        try (OutputStream os = new FileOutputStream(getDecodePath());
             Writer writer =new OutputStreamWriter(os)){
            final Properties properties = getProperties();
            if(Objects.nonNull(properties)){
                properties.store(writer, "flush config");
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    private Properties getProperties(){
        if(properties == null){
            synchronized (this){
                if(properties == null){
                    final String decodePath = getDecodePath();
                    try (final FileInputStream fileInputStream = new FileInputStream(decodePath)){
                        properties = new Properties();
                        properties.load(fileInputStream);
                    } catch (IOException e) {
                        log.error(e.getMessage(), e);
                        properties = null;
                        return null;
                    }
                }
            }
        }
        return properties;
    }

    private String getDecodePath() {
        try {
            return URLDecoder.decode(path, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage(), e);
        }
        return path;
    }
}
